﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CDSBDemandControl.Model;
using CDSBDemandControl.Configuration;

namespace CDSBDemandControl.Communication.Translator
{
    public class DemandModelTranslator
    {
        public static List<DemandModel> Translate(DemandCollection demandCollection)
        {
            List<DemandModel> DemandModelList = new List<DemandModel>();
            DemandModel demandModel = null;

            foreach (XMLDemandModel serviceDemandModel in demandCollection)
            {
                if (serviceDemandModel.WorkItemType == "Scenario")
                    demandModel = TranslateScenario(serviceDemandModel);
                else
                {
                    demandModel = new DemandModel();

                    demandModel.ID = serviceDemandModel.ID;
                    demandModel.Title = serviceDemandModel.Title;
                    demandModel.State = serviceDemandModel.State;
                    demandModel.WorkItemType = serviceDemandModel.WorkItemType;
                    demandModel.Project = serviceDemandModel.Project;
                    demandModel.Reason = serviceDemandModel.Reason;
                    demandModel.CreatedBy = serviceDemandModel.CreatedBy;
                    demandModel.AssignedTo = serviceDemandModel.AssignedTo;
                    demandModel.Revision = serviceDemandModel.Revision;
                    demandModel.DataIncFila = serviceDemandModel.DataIncFila != DateTime.MinValue ? (DateTime?)serviceDemandModel.DataIncFila : null;
                    demandModel.EstimatedStart = serviceDemandModel.EstimatedStart != DateTime.MinValue ? (DateTime?)serviceDemandModel.EstimatedStart : null;
                    demandModel.EstimatedConclude = serviceDemandModel.EstimatedConclude != DateTime.MinValue ? (DateTime?)serviceDemandModel.EstimatedConclude : null;
                    demandModel.Start = serviceDemandModel.Start != DateTime.MinValue ? (DateTime?)serviceDemandModel.Start : null;
                    demandModel.Conclude = serviceDemandModel.Conclude != DateTime.MinValue ? (DateTime?)serviceDemandModel.Conclude : null;
                    demandModel.Origin = serviceDemandModel.Origin.ToEnumOrigin();
                    demandModel.Priority = serviceDemandModel.Priority.ToEnumPriority();
                    demandModel.Gravity = serviceDemandModel.Gravity.ToEnumGravity();
                    demandModel.Remedy = serviceDemandModel.Remedy;
                    demandModel.Justification = serviceDemandModel.Justification;
                    demandModel.Description = serviceDemandModel.Description;
                    demandModel.IsSimultaneous = serviceDemandModel.IsSimultaneous;

                    if (!string.IsNullOrEmpty(serviceDemandModel.Description))
                        demandModel.Description = serviceDemandModel.Description;

                        demandModel.EstimatedHours = serviceDemandModel.EstimatedHours;
                        demandModel.UsedHours = serviceDemandModel.UsedHours;
                }

                DemandModelList.Add(demandModel);
            }

            return DemandModelList;
        }

        private static DemandModel TranslateScenario(XMLDemandModel serviceDemandModel)
        {
            if (serviceDemandModel == null) return null;

            DemandModel demanda = new DemandModel();

            demanda.ID = serviceDemandModel.ID;
            demanda.Title = serviceDemandModel.Title;
            demanda.State = serviceDemandModel.State;
            demanda.WorkItemType = serviceDemandModel.WorkItemType;
            demanda.Project = serviceDemandModel.Project;
            demanda.Reason = serviceDemandModel.Reason;
            demanda.CreatedBy = serviceDemandModel.CreatedBy;
            demanda.AssignedTo = serviceDemandModel.AssignedTo;
            demanda.EstimatedHours = serviceDemandModel.EstimatedHours;
            demanda.UsedHours = serviceDemandModel.UsedHours;
            demanda.Remedy = serviceDemandModel.Remedy;
            demanda.Revision = serviceDemandModel.Revision;
            demanda.DataIncFila = serviceDemandModel.DataIncFila;

            demanda.Origin = serviceDemandModel.Origin.ToEnumOrigin();
            demanda.Priority = serviceDemandModel.Priority.ToEnumPriority();
            demanda.Gravity = serviceDemandModel.Gravity.ToEnumGravity();
                    
            demanda.Description = "" + serviceDemandModel.Description;

            #region Em avaliação de demanda

            demanda.AssessingDemandPlannedStartDate = serviceDemandModel.AssessingDemandPlannedStartDate;
            demanda.AssessingDemandPlannedEndDate = serviceDemandModel.AssessingDemandPlannedEndDate;
            demanda.AssessingDemandPlannedTime = serviceDemandModel.AssessingDemandPlannedTime;
            demanda.AssessingDemandRealizedStartDate = serviceDemandModel.AssessingDemandRealizedStartDate;
            demanda.AssessingDemandRealizedEndDate = serviceDemandModel.AssessingDemandRealizedEndDate;
            demanda.AssessingDemandRealizedTime = serviceDemandModel.AssessingDemandRealizedTime;            

            #endregion

            #region Especificação(Geral)

            demanda.GeneralPlannedStartDate = serviceDemandModel.GeneralPlannedStartDate;
            demanda.GeneralPlannedEndDate = serviceDemandModel.GeneralPlannedEndDate;
            demanda.GeneralPlannedTime = serviceDemandModel.GeneralPlannedTime;
            demanda.GeneralPlannedComplexity = serviceDemandModel.GeneralPlannedComplexity;
            demanda.GeneralRealizedStartDate = serviceDemandModel.GeneralRealizedStartDate;
            demanda.GeneralRealizedEndDate = serviceDemandModel.GeneralRealizedEndDate;
            demanda.GeneralRealizedTime = serviceDemandModel.GeneralRealizedTime;
            demanda.GeneralRealizedEvolutions = serviceDemandModel.GeneralRealizedEvolutions;

            #endregion

            #region Especificação(Artefatos)(Artefatos de Testes)

            demanda.MEDRTFPlannedStartDate = serviceDemandModel.MEDRTFPlannedStartDate;
            demanda.MEDRTFPlannedEndDate = serviceDemandModel.MEDRTFPlannedEndDate;
            demanda.MEDRTFPlannedTime = serviceDemandModel.MEDRTFPlannedTime;
            demanda.MEDRTFPlannedMassData = serviceDemandModel.MEDRTFPlannedMassData;
            demanda.MEDRTFRealizedStartDate = serviceDemandModel.MEDRTFRealizedStartDate;
            demanda.MEDRTFRealizedEndDate = serviceDemandModel.MEDRTFRealizedEndDate;
            demanda.MEDRTFRealizedTime = serviceDemandModel.MEDRTFRealizedTime;
            demanda.MEDRTFRealizedEvolutions = serviceDemandModel.MEDRTFRealizedEvolutions;

            #endregion

            #region Especificação(Artefatos)(Diagramação)

            demanda.XAMLPlannedStartDate = serviceDemandModel.XAMLPlannedStartDate;
            demanda.XAMLPlannedEndDate = serviceDemandModel.XAMLPlannedEndDate;
            demanda.XAMLPlannedTime = serviceDemandModel.XAMLPlannedTime;
            demanda.XAMLRealizedStartDate = serviceDemandModel.XAMLRealizedStartDate;
            demanda.XAMLRealizedEndDate = serviceDemandModel.XAMLRealizedEndDate;
            demanda.XAMLRealizedTime = serviceDemandModel.XAMLRealizedTime;
            demanda.XAMLRealizedEvolutions = serviceDemandModel.XAMLRealizedEvolutions;

            #endregion

            #region Arquitetura

            demanda.ArchitecturePlannedStartDate = serviceDemandModel.ArchitecturePlannedStartDate;
            demanda.ArchitecturePlannedEndDate = serviceDemandModel.ArchitecturePlannedEndDate;
            demanda.ArchitecturePlannedTime = serviceDemandModel.ArchitecturePlannedTime;
            demanda.ArchitecturePlannedComplexity = serviceDemandModel.ArchitecturePlannedComplexity;
            demanda.ArchitectureRealizedStartDate = serviceDemandModel.ArchitectureRealizedStartDate;
            demanda.ArchitectureRealizedEndDate = serviceDemandModel.ArchitectureRealizedEndDate;
            demanda.ArchitectureRealizedTime = serviceDemandModel.ArchitectureRealizedTime;
            demanda.ArchitectureRealizedEvolutions = serviceDemandModel.ArchitectureRealizedEvolutions;

            #endregion

            #region Desenvolvimento

            demanda.DevelopmentPlannedStartDate = serviceDemandModel.DevelopmentPlannedStartDate;
            demanda.DevelopmentPlannedEndDate = serviceDemandModel.DevelopmentPlannedEndDate;
            demanda.DevelopmentPlannedTime = serviceDemandModel.DevelopmentPlannedTime;
            demanda.DevelopmentPlannedComplexity = serviceDemandModel.DevelopmentPlannedComplexity;
            demanda.DevelopmentRealizedStartDate = serviceDemandModel.DevelopmentRealizedStartDate;
            demanda.DevelopmentRealizedEndDate = serviceDemandModel.DevelopmentRealizedEndDate;
            demanda.DevelopmentRealizedTime = serviceDemandModel.DevelopmentRealizedTime;
            demanda.DevelopmentRealizedEvolutions = serviceDemandModel.DevelopmentRealizedEvolutions;

            #endregion

            #region Testes

            demanda.TestsPlannedStartDate = serviceDemandModel.TestsPlannedStartDate;
            demanda.TestsPlannedEndDate = serviceDemandModel.TestsPlannedEndDate;
            demanda.TestsPlannedTime = serviceDemandModel.TestsPlannedTime;
            demanda.TestsRealizedStartDate = serviceDemandModel.TestsRealizedStartDate;
            demanda.TestsRealizedEndDate = serviceDemandModel.TestsRealizedEndDate;
            demanda.TestsRealizedTime = serviceDemandModel.TestsRealizedTime;
            demanda.TestCiclo = serviceDemandModel.TestCiclo;

            #endregion

            #region Aceite

            demanda.AcceptedPlannedStartDate = serviceDemandModel.AcceptedPlannedStartDate;
            demanda.AcceptedPlannedEndDate = serviceDemandModel.AcceptedPlannedEndDate;
            demanda.AcceptedPlannedTime = serviceDemandModel.AcceptedPlannedTime;
            demanda.AcceptedRealizedStartDate = serviceDemandModel.AcceptedRealizedStartDate;
            demanda.AcceptedRealizedEndDate = serviceDemandModel.AcceptedRealizedEndDate;
            demanda.AcceptedRealizedTime = serviceDemandModel.AcceptedRealizedTime;
            demanda.AcceptedRealizedEvolutions = serviceDemandModel.AcceptedRealizedEvolutions;

            #endregion

            #region Release

            demanda.ReleasePlannedStartDate = serviceDemandModel.ReleasePlannedStartDate;
            demanda.ReleasePlannedEndDate = serviceDemandModel.ReleasePlannedEndDate;
            demanda.ReleasePlannedTime = serviceDemandModel.ReleasePlannedTime;
            demanda.ReleaseRealizedStartDate = serviceDemandModel.ReleaseRealizedStartDate;
            demanda.ReleaseRealizedEndDate = serviceDemandModel.ReleaseRealizedEndDate;
            demanda.ReleaseRealizedTime = serviceDemandModel.ReleaseRealizedTime;
            demanda.ReleaseRealizedEvolutions = serviceDemandModel.ReleaseRealizedEvolutions;

            #endregion

            return demanda;
        }
    }
}